package cn.fz.relaxation.job.baidu;

import cn.fz.relaxation.common.util.MyUtil;
import cn.fz.relaxation.dao.entity.RealxHotSearchDO;
import cn.fz.relaxation.job.BaseHotSearchJob;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static cn.fz.relaxation.common.enums.HotSearchEnum.BAIDU;

@Component
@Slf4j
public class BaiduHotSearchJob extends BaseHotSearchJob {
    /**
     * 定时触发爬虫方法，1个小时执行一次
     */
    // @Scheduled(fixedRate = 1000 * 60 *60)
    // @Scheduled(fixedRate = 3000)
    // @Scheduled(cron = "49 19 11 * * ?")
    @Override
    @XxlJob("baiduHotSearchJob")
    public ReturnT<String> hotSearch(String param) throws IOException {
        log.info("百度热搜爬虫任务开始");
        try {
            this.resourceCode = BAIDU.getCode();
            // 获取百度热搜
            String url = "https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065";
            Document doc = Jsoup.connect(url).get();
            // 标题
            Elements titles = doc.select(".c-single-text-ellipsis");
            // 图片
            Elements imgs = doc.select(".category-wrap_iQLoo .index_1Ew5p").next("img");
            // 内容
            Elements contents = doc.select(".hot-desc_1m_jR.large_nSuFU");
            // 推荐图
            Elements urls = doc.select(".category-wrap_iQLoo a.img-wrapper_29V76");
            // 热搜指数
            Elements levels = doc.select(".hot-index_1Bl1a");
            for (int i = 0; i < levels.size(); i++) {
                RealxHotSearchDO searchDO = RealxHotSearchDO.builder().hotSearchResource(resourceCode).build();
                // 设置文章标题
                searchDO.setHotSearchTitle(titles.get(i).text().trim());
                // 设置百度三方ID
                searchDO.setHotSearchId(MyUtil.getHashId(resourceCode + searchDO.getHotSearchTitle()));
                // 设置文章封面
                searchDO.setHotSearchCover(imgs.get(i).attr("src"));
                // 设置文章摘要
                searchDO.setHotSearchExcerpt(contents.get(i).text().replaceAll("查看更多>",""));
                // 设置文章连接
                searchDO.setHotSearchUrl(urls.get(i).attr("href"));
                // 设置热搜热度
                searchDO.setHotSearchHeat(levels.get(i).text().trim());
                // 按顺序排名
                searchDO.setHotSearchOrder(i + 1);
                // 添加到集合
                searchDOList.add(searchDO);
            }
            if (CollectionUtils.isEmpty(searchDOList)){
                log.error("没有获取到百度热搜数据");
                return ReturnT.SUCCESS;
            }
            // 数据加到缓存和数据持久化
            this.saveSearchListToDBAndMap();
            log.info("获取百度热搜数据爬虫任务结束");
        } catch (IOException e) {
            log.error("获取百度热搜数据异常", e);
            // throw new RuntimeException(e);
        }
        return ReturnT.SUCCESS;
    }
}
